package com.example.tliasmanagement.service.impl;

import com.example.tliasmanagement.dto.AddClazzDto;
import com.example.tliasmanagement.dto.ClazzListDto;
import com.example.tliasmanagement.dto.UpdateClazzDto;
import com.example.tliasmanagement.entity.Clazz;
import com.example.tliasmanagement.entity.Student;
import com.example.tliasmanagement.exception.BizException;
import com.example.tliasmanagement.mapper.ClazzMapper;
import com.example.tliasmanagement.service.ClazzService;
import com.example.tliasmanagement.vo.EmpVo;
import com.example.tliasmanagement.vo.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageBean clazzs(ClazzListDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<Clazz> list = clazzMapper.page(dto);

        //获取状态
        for (Clazz clazz : list) {
            if (clazz.getBeginDate().isAfter(LocalDate.now())) {
                clazz.setStatus("未开班");
            } else if (LocalDate.now().isAfter(clazz.getEndDate())) {
                clazz.setStatus("已结束");
            } else if (clazz.getEndDate().isAfter(LocalDate.now()) && LocalDate.now().isAfter(clazz.getBeginDate())) {
                clazz.setStatus("正在进行");
            }
        }

        Page<Clazz> p = (Page<Clazz>) list;
        return new PageBean(p.getTotal(), p.getResult());
    }
    /*public PageBean<ClazzListVo> clazzs1(ClazzListDto dto){
        int count = clazzMapper.totalCount();
        int start = (dto.getPage() - 1) * dto.getPageSize();
        List<ClazzListVo> rows = clazzMapper.selectPage(start, dto.getPageSize());
        return new PageBean(Long.valueOf(count),rows);
    }*/

    /*@Override
    @Transactional(rollbackFor = Exception.class)
    public void addClazz(AddClazzDto dto) {
        Clazz clazz = clazzMapper.selectByUsername(dto.getUsername());
        if (clazz != null){
            throw new BizException("用户名重复");
        }
    }*/

    @Override
    public void addClazz(AddClazzDto dto) {
        Clazz clazz = new Clazz();
        BeanUtils.copyProperties(dto, clazz);
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(clazz);
    }

    @Override
    public List<EmpVo> getName() {
        List<EmpVo> list = clazzMapper.selectAll();
        return list;
    }


    @Override
    public void deleteById(Integer id) {
        Clazz clazzDB = clazzMapper.selectById(id);
        if(clazzDB == null){
            throw new BizException("班级不存在");
        }
        // 判断是否有学生
        List<Student> list = clazzMapper.getClazzId(id);
        if (!CollectionUtils.isEmpty(list)) {
            throw new BizException("对不起, 该班级下有学生, 不能直接删除");
        }else {
            clazzMapper.deleteById(id);
        }

    }


    @Override
    public Clazz getById(Integer id) {
        Clazz clazz = clazzMapper.selectById(id);
        return clazz;
    }

    @Override
    public void updateClazz(UpdateClazzDto dto) {
        Clazz clazz = new Clazz();
        clazz.setName(dto.getName());
        clazz.setRoom(dto.getRoom());
        clazz.setBeginDate(LocalDate.now());
        clazz.setEndDate(LocalDate.now());
        clazz.setMasterId(dto.getMasterId());
        clazz.setSubject(dto.getSubject());
        clazz.setId(dto.getId());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }


}
